package com.order.manager.controller.Feedback;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.manager.Model.ComplaintInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.service.impl.ComplaintInfoServiceImpl;
import com.order.manager.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    @Resource
    ComplaintInfoServiceImpl complaintInfoService;

    @Resource
    JwtUtil jwtUtil;

    @RequestMapping("/create")
    public RestBean createComplaint(String content, HttpServletRequest request) {
        String uuid = jwtUtil.getJwtUsername(request);
        ComplaintInfo complaintInfo = new ComplaintInfo();
        complaintInfo.setInputTime(new Date());
        complaintInfo.setUserId(uuid);
        complaintInfo.setContent(content);
        complaintInfo.setState(1);
        return complaintInfoService.save(complaintInfo) ? RestBean.success(complaintInfo) : RestBean.failure(500, "反馈失败");
    }

    @RequestMapping("/select")
    public RestBean viewMyBack(HttpServletRequest request){
        String uuid = jwtUtil.getJwtUsername(request);
        List<ComplaintInfo> complaintInfos = complaintInfoService.list(new QueryWrapper<ComplaintInfo>()
                .eq("user_id", uuid)
        );
        return RestBean.success(complaintInfos);
    }

    /**
     * 反馈信息反馈
     * @param complaintId
     * @param content
     * @return
     */
    @RequestMapping("/freeback/{complaintId}")
    public RestBean freebackComplaint(@PathVariable int complaintId, String content) {
        ComplaintInfo complaintInfo = complaintInfoService.
                getById(complaintId);
        if (complaintInfo != null ){
            complaintInfo.setUpdateTime(new Date());
            complaintInfo.setFeedbackContent(content);
            return complaintInfoService.updateById(complaintInfo)?RestBean.success(complaintInfo):RestBean.failure(500,"反馈失败");
        }
        return RestBean.failure(403,"反馈记录丢失");
    }







}
